package com.qf.web.controller;


import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;
import com.qf.utils.Base64Utils;
import com.qf.utils.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/userservlet")
public class UserServlet extends BaseServlet {

    ApplicationContext app=new ClassPathXmlApplicationContext("/spring-context.xml");
    UserService userService = (UserService) app.getBean("userService");

    public String userRegist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if (StringUtils.isEmpty(username)){
            request.setAttribute("error","用户名不能为空");
            return "/error.jsp";
        }
        if (StringUtils.isEmpty(password)){
            request.setAttribute("error","密码不能为空");
            return "/error.jsp";
        }
        password= Base64Utils.encode(password);
        try {
            User user=new User(username,password,new Integer(1));
            userService=new UserServiceImpl();
            userService.regist(user);
            return "redirect:/login.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("error",e);
            return "/error.jsp";
        }

    }

    public String userCheck(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if (StringUtils.isEmpty(username)){
            response.getWriter().write("1");
            return null;
        }
        if (StringUtils.isEmpty(password)){
            response.getWriter().write("2");
            return null;
        }


        return null;
    }

    public String userLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if (StringUtils.isEmpty(username)){
            request.setAttribute("error","用户名不能为空");
            return "/error.jsp";
        }
        if (StringUtils.isEmpty(password)){
            request.setAttribute("error","密码不能为空");
            return "/error.jsp";
        }

        password=Base64Utils.encode(password);
        try {
            User user=userService.login(username,password);
            request.getSession().setAttribute("user",user);
            return "redirect:/profileservlet?method=listProfile";
        } catch (Exception e) {
            request.setAttribute("error",e);
            return "/error.jsp";
        }

    }

}
